package net.zhengxinyun.performance.service.measurement;

import net.zhengxinyun.performance.Util.DateUtils;
import net.zhengxinyun.performance.Util.StringUtils;
import net.zhengxinyun.performance.entity.AnswerEntity;
import net.zhengxinyun.performance.entity.EvaluateUserEntity;
import net.zhengxinyun.performance.entity.MeasurementInformationEntity;
import net.zhengxinyun.performance.filter.AnswerFilter;
import net.zhengxinyun.performance.filter.EvaluateUserFilter;
import net.zhengxinyun.performance.filter.MeasurementInformationFilter;
import net.zhengxinyun.performance.mapper.login.EvaluateUserEntityMapper;
import net.zhengxinyun.performance.mapper.measurement.AnswerEntityMapper;
import net.zhengxinyun.performance.mapper.measurement.MeasurementInformationEntityMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * Created by hyp1202 on 2018/12/29.
 */
@Service
@Transactional
public class AnswerServiceImpl implements AnswerService {

    private MeasurementInformationEntityMapper measurementInformationEntityMapper;
    private AnswerEntityMapper answerEntityMapper;
    private EvaluateUserEntityMapper evaluateUserEntityMapper;

    @Autowired
    public AnswerServiceImpl(MeasurementInformationEntityMapper measurementInformationEntityMapper, AnswerEntityMapper answerEntityMapper, EvaluateUserEntityMapper evaluateUserEntityMapper) {
        this.measurementInformationEntityMapper = measurementInformationEntityMapper;
        this.answerEntityMapper = answerEntityMapper;
        this.evaluateUserEntityMapper = evaluateUserEntityMapper;
    }

    @Override
    public String insert(MeasurementInformationEntity measurementInformationEntity,List<AnswerEntity> list) {
        //获取1-100之间的随机数
        int random = 1 + (int) (Math.random() * 99);
        //根据当前时间和随机数生成流水号。
        String evaluateUserId = DateUtils.getDateTimeString() + StringUtils.numberFormat(false, 2, 2, random);
        for (AnswerEntity answerEntity:list) {
            answerEntity.setRespondents(evaluateUserId);
            answerEntity.setMeasurementId(measurementInformationEntity.getId());
            int i = answerEntityMapper.insert(answerEntity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "答题失败，请联系管理员";
            }
        }
        return "答题成功";
    }

    @Override
    public String insert(MeasurementInformationEntity measurementInformationEntity,List<AnswerEntity> list, EvaluateUserEntity evaluateUserEntity) {
        for (AnswerEntity answerEntity:list) {
            answerEntity.setRespondents(evaluateUserEntity.getPhone());
            answerEntity.setMeasurementId(measurementInformationEntity.getId());
            int i = answerEntityMapper.insert(answerEntity);
            if (i == 0){
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return "答题失败，请联系管理员";
            }
        }
        return "答题成功";
    }

    @Override
    public String insertList(MeasurementInformationEntity measurementInformationEntity, List<AnswerEntity> list) {
        return null;
    }

    @Override
    public String insertList(MeasurementInformationEntity measurementInformationEntity, List<AnswerEntity> list, EvaluateUserEntity evaluateUserEntity) {
        return null;
    }

    @Override
    public String update(MeasurementInformationEntity measurementInformationEntity, AnswerEntity answerEntity) {
        return null;
    }

    @Override
    public String delete(MeasurementInformationEntity measurementInformationEntity) {
        return null;
    }

    @Override
    public Map<String, Object> query(AnswerFilter answerFilter) {
        return null;
    }

    /**
     * 有测评对象的登陆
     * @param phone
     * @return
     */
    @Override
    public List<MeasurementInformationFilter> login(String phone) {
        EvaluateUserFilter filter = evaluateUserEntityMapper.queryByPhone(phone);
        if (filter == null){
            return null;
        }
        MeasurementInformationFilter measurementInformationFilter = new MeasurementInformationFilter();
        measurementInformationFilter.setTestObj(filter.getDepartment());
        measurementInformationFilter.setType("0");
        measurementInformationFilter.setStatus("1");
        List<MeasurementInformationFilter> list = measurementInformationEntityMapper.query(measurementInformationFilter);
        return list;
    }

    /**
     * 没有测评对象的
     * @return
     */
    @Override
    public List<MeasurementInformationFilter> noLogin() {
        MeasurementInformationFilter measurementInformationFilter = new MeasurementInformationFilter();
        measurementInformationFilter.setType("1");
        measurementInformationFilter.setStatus("1");
        List<MeasurementInformationFilter> list = measurementInformationEntityMapper.query(measurementInformationFilter);
        return list;
    }


    @Override
    public List<AnswerFilter> queryForAnswerCount(AnswerFilter answerFilter) {
        List<AnswerFilter> list = answerEntityMapper.queryForAnswerCount(answerFilter);
        //list自带函数根据问题id来分组
//        Map<Long,List<AnswerFilter>> map = list.stream().collect(Collectors.groupingBy(AnswerFilter::getQuestionId));
//        System.out.println(map);
        return list;
    }


}
